﻿using CPF;
using CPF.Animation;
using CPF.Charts;
using CPF.Controls;
using CPF.Drawing;
using CPF.Shapes;
using CPF.Styling;
using CPF.Svg;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

namespace CPFUI.IControl
{
    [CPF.Design.DesignerLoadStyle("res://$safeprojectname$/Stylesheet1.css")]//用于设计的时候加载样式
    public class MenuControl : Control
    {
        public Color GroupForeground
        {
            get { return GetValue<Color>(); }
            set
            {
                SetValue(value);
            }
        }
        public Color ItemNormalForeground
        {
            get { return GetValue<Color>(); }
            set
            {
                SetValue(value);
            }
        }
        public Color ItemSelectedForeground
        {
            get { return GetValue<Color>(); }
            set
            {
                SetValue(value);
            }
        }
        public MenuControlItem SelectedItem;
        public StackPanel ListBox;
        public MenuControl()
        {
            ListBox = new StackPanel()
            {
                Orientation = Orientation.Vertical,
                MarginTop = 0,
                MarginLeft = 0,
                MarginRight = 0,
            };
        }
        //模板定义
        protected override void InitializeComponent()
        {
            
            IsAntiAlias = true;
            ScrollViewer sv = new ScrollViewer
            {
                MarginLeft = 0,
                MarginRight = 0,
                MarginTop = 0,
                MarginBottom = 0,
                Content = ListBox,
            };
            Children.Add(sv);
        }

        public void AddGroup(MenuGroupItem group)
        {
            group.Foreground= GroupForeground;
            group.MarginLeft = 0;
            group.MarginRight = 0;
            ListBox.Children.Add(group);
            for (int i = 0; i < group.MenuList.Children.Count; i++) {
                MenuControlItem item = group.MenuList.Children[i] as MenuControlItem;
                item.Foreground = ItemNormalForeground;
                item.MouseDown += IMenuControl_MouseDown;
            }
        }

        private void IMenuControl_MouseDown(object sender, CPF.Input.MouseButtonEventArgs e)
        {
            MenuControlItem item = sender as MenuControlItem;
            if (SelectedItem != null)
            {
                SelectedItem.Foreground = ItemNormalForeground;
                SelectedItem.Background = Color.FromArgb(0, 0, 0, 0);
            }
            SelectedItem = item;
            item.Background = Color.FromArgb(80, 0, 0, 0);
            item.Foreground = ItemSelectedForeground;
        }
    }
}
